
clear all

use "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/data_used_in_main_regs.dta", clear
keep if !missing(pricediff)
duplicates drop delivery_point_barcode, force
codebook delivery_point_barcode

cap drop *_st

merge 1:m delivery_point_barcode using "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/noncompliers.dta", force

cap drop *_st

gen main_sample=0
replace main_sample = 1 if noncomplier==0


local less_observable_vars="atticrvalue ductravg duct_type negleakavg rsavg  eeravg"
local more_observable_vars="ah_location sizeavg progtherm fftype whftype negsysageavg"
local other_less_observable_vars="ah_type notwinrec"
local other_more_observable_vars="twosystems notraddr"

cap gen fftype=furnacefueltype
replace fftype=furnacefueltype if missing(fftype)

cap gen whftype=waterheaterfueltype
replace whftype=waterheaterfueltype if missing(whftype)

cap gen ah_location=system1locationairhandler
replace ah_location=system1locationairhandler if missing(ah_location)

cap gen ah_type=system1airhandlertype
replace ah_type=system1airhandlertype if missing(ah_type)


cap gen duct_type=ductsystem1type
replace duct_type=ductsystem1type if missing(ah_type)

local parsimonious_less_vars "negleakavg atticrvalue ductravg     "
local parsimonious_more_vars_fuel "fftype whftype "
local parsimonious_more_vars_other "ah_location progtherm twosystems"

local allvars "`less_observable_vars' `more_observable_vars' `other_less_observable_vars' `other_more_observable_vars'"

sum main_sample

cap gen ah_location=system1locationairhandler
cap gen ah_type=system1airhandlertype
cap gen wh_tanktype=waterheatertanktype

cap rename ductsystem1type duct_type
cap rename ductsystem1type duct_type


sum notwinrec 
cap gen notwinrec_st=(notwinrec-r(mean))/r(sd)
replace notwinrec_st=(notwinrec-r(mean))/r(sd)

sum twosystems
cap gen twosystems_st=(twosystems-r(mean))/r(sd)
replace twosystems_st=(twosystems-r(mean))/r(sd)


cap gen notraddr=(rrec==0) if !missing(rrec) 
sum notraddr
cap gen notraddr_st=(notraddr-r(mean))/r(sd)

cap rename system1airhandlertype ah_type
cap rename system1locationairhandler ah_location
cap rename ductsystem1type_st duct_type_st

local list_to_standardize `allvars'


*some of these don't have the standardized values
foreach w in `list_to_standardize'  {
	sum `w' 
	*cap drop `w'_st
	cap gen  `w'_st=(`w'-r(mean))/(r(sd))
	replace `w'_st=(`w'-r(mean))/(r(sd)) if missing(`w'_st)
	la var `w'_st "`: var label `w''"
	}

cap drop _merge
	cap drop dum
	cap gen dum=1
	
	merge m:1 dum using "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/load_tt_from_test.dta"
	
	gen Less=0
	gen More_Fuel=0
	gen More_Other=0
	foreach v in `parsimonious_less_vars' {
		replace Less = Less+ `v'_st*`v'_Less
	}
	foreach v in `parsimonious_more_vars_fuel' {
		replace More_Fuel = More_Fuel+ `v'_st*`v'_More_Fuel
	}
	foreach v in `parsimonious_more_vars_other' {
		replace More_Other = More_Other+ `v'_st*`v'_More_Other
	}

sum Less, detail	
gen above_median_less=(Less>r(p50)) if !missing(Less)
tab above_median_less

la var Less "Less"
la var More_Other "More-Other"
la var More_Fuel "More-Fuel"

la var wh_tanktype "Tankless or Solar WH"

la var notraddr "Did not Rec Add Attic R"
la var negleakavg "$-$ \% Leakage"
la var atticrvalue "Attic R-value"


la var negleakavg "$-$ \% Duct Leakage"

la var duct_type "Metal Ducts"
la var twosystems "2 Systems"
la var fftype "Gas Furnace"
la var ah_type "Vertical AH"
la var whftype "Gas WH"
la var ah_location "AH in Closet"

la var eeravg "EER"
la var negleakavg "$-$ \% Duct Leakage"
la var atticrvalue "Attic R-value"

la var rsavg "Return Sizing Adequate"
la var ductravg "Duct R-value"

la var negsysageavg "$-$ System Age"

la var sizeavg "HVAC Size (sqft/ton)"
la var notwinrec "Did Not Rec WS"

la var progtherm "Programmable Thermostat"
la var rrec "Amount of Recommended Additional R-value"
la var notraddr "Did Not Rec Att R"

local list_to_standardize Less More_Fuel More_Other 


foreach w in `list_to_standardize'  {
	sum `w' 
	gen `w'_st=(`w'-r(mean))/(r(sd))
	la var `w'_st "`: var label `w''"
	}


gen other_audited=1 if main_sample==0
replace other_audited=0 if main_sample==1
sum other_audited


set scheme plotplainblind

********************************************************************************
*Table A11 compliers vs late compliers, ee features.
********************************************************************************
eststo clear
estimates clear

local list_to_compare Less_st ///
	 More_Fuel ///
	 More_Other ///
	 `allvars'


	 
stddiff  `list_to_compare' , ///
	 by(noncomplier)
	 


local words= `: word count `list_to_compare''

mat output=r(output)

mat c_1=output[1..`words',5]

mat li c_1

*second col is variance ratios.
*initialize it.
mat c_2=c_1


forval i=1/`words' {
	mat c_2[`i',1] = (output[`i',2])^2/(output[`i',4])^2
	}
mat li c_1
mat li c_2

mat R=[c_1, c_2]
mat li R

ereturn list

eststo clear

estadd matrix R




esttab e(R, fmt(%9.3fc)) using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/compliers_noncompliers_std_diff.tex", ///
	label nodepvar replace coeflabels(`e(labels)') nomtitles ///
	collabels(none) ///
	b(%9.3fc) ///
	refcat(Less_st  " \addlinespace \textbf{EE Indexes:}" atticrvalue " \addlinespace \textbf{Audit Features:}", nolabel) ///
	mlabels(none) eqlabels(none) booktabs fragment
	